<template>
  <div class="root">
    <el-table :data="tableData" :border="parentBorder" style="width: 100%">
      <el-table-column type="expand">
        <template #default="props">
          <div m="4">
            <el-table :data="props.row.family" :border="childBorder">
              <el-table-column label="名称" prop="name" />
              <el-table-column label="出版日期" prop="state" />
              <el-table-column label="作者" prop="city" />
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="日期" prop="date" />
      <el-table-column label="分类" prop="name" />
    </el-table>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { userLIst } from "@/api/home/index";
import { ElMessage } from "element-plus";
let list1 = reactive([
  {
    key: 1,
    name: "忍村",
  },
  {
    key: 2,
    name: "火村",
  },
  {
    key: 3,
    name: "土村",
  },
]);

const tableData = ref([]);
const deleteRow = (index) => {
  tableData.value.splice(index, 1);
};
let dialogTittle = ref("");
let dialogVisible = ref(false);
let editRow = (e) => {
  dialogTittle.value = "编辑";
  submitFormdata.value = JSON.parse(JSON.stringify(e));
  dialogVisible.value = true;
};
const onAddItem = () => {
  dialogTittle.value = "新增";
  submitFormdata.value = {};
  dialogVisible.value = true;
};

let ruleFormRef = ref(null);
const rules = reactive({
  time: [{ required: true, message: "请填写时间" }],
  name: [{ required: true, message: "请填写名称" }],
});
let submitFormdata = ref({});
const submitForm = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      console.log("submit!");
      if (dialogTittle.value == "编辑") {
        tableData.value.forEach((item, index) => {
          if (item.code == submitFormdata.value.code) {
            tableData.value[index] = { ...item, ...submitFormdata.value };
          }
        });
        dialogVisible.value = false;
        console.log(tableData.value);
        ElMessage.success("操作成功");
      }
      if (dialogTittle.value == "新增") {
        submitFormdata.value.code = Math.floor(Math.random() * 9000) + 1000;
        dialogVisible.value = false;
        tableData.value.push(submitFormdata.value);
        ElMessage.success("操作成功");
        submitFormdata.value = {};
      }
    } else {
      ElMessage.error("请完成表单");
    }
  });
};

const resetForm = () => {
  submitFormdata.value = {};
  dialogVisible.value = false;
};
let query = ref({ name: "" });

onMounted(() => {
  tableData.value = [
    {
      date: "2016-05-03",
      name: "四大名著",
      state: "California",
      city: "San Francisco",
      address: "3650 21st St, San Francisco",
      zip: "CA 94114",
      family: [
        {
          name: "西游记",
          state: "2024-6-13",
          city: "施耐庵",
        },
        {
          name: "水浒传",
          state: "2024-6-13",
          city: "范冲淹",
        },
        {
          name: "三国演义",
          state: "2024-6-13",
          city: "范冲淹",
        },
      ],
    },
    {
      date: "2016-05-03",
      name: "英文",
      state: "California",
      city: "San Francisco",
      address: "3650 21st St, San Francisco",
      zip: "CA 94114",
      family: [
        {
          name: "飘",
          state: "2024-6-13",
          city: "罗密欧",
        },
      ],
    },
    {
      date: "2016-05-03",
      name: "法国",
      state: "California",
      city: "San Francisco",
      address: "3650 21st St, San Francisco",
      zip: "CA 94114",
      family: [
        {
          name: "刺客信条",
          state: "2024-6-13",
          city: "育碧",
        },
      ],
    },
  ];
});
</script>

<style lang="scss" scoped>
.root {
  width: 100%;
}
.head {
  margin-bottom: 20px;
  .searchItem {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
</style>
